<template>
  <div class="mod-demo__project" style="overflow-y: scroll;height: 800px;" v-show="view.showTable" >
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目名称">
            <el-input v-model="state.dataForm.name" placeholder="请输入项目名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="施工负责人">
            <el-input v-model="state.dataForm.projectHead" placeholder="请输入施工负责人" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属区域">
            <el-row :gutter="24">
              <el-col :span="8">
                <el-select v-model="state.dataForm.provinceId" clearable @change="provinceChange" placeholder="请选择省">
                  <!---->
                  <el-option v-for="item in view.provinceOP" :key="item.id" :label="item.name"
                             :value="item.id"></el-option>
                </el-select>
              </el-col>
              <el-col :span="8">
                <el-select v-model="state.dataForm.cityId" clearable @change="cityChange" placeholder="请选择市"><!--"-->
                  <el-option v-for="item in view.provOp" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-col>
              <el-col :span="8">
                <el-select v-model="state.dataForm.creaId" clearable placeholder="请选择区">
                  <el-option v-for="item in view.creaOp" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目状态">
            <el-select clearable v-model="state.dataForm.state" placeholder="请选择项目状态">
              <el-option label="施工中" value="0"></el-option>
              <el-option label="待验收" value="1"></el-option>
              <el-option label="已验收" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button @click="state.getDataList()">查询</el-button>
          </el-form-item>
          <el-form-item>
            <!--        v-if="state.hasPermission('sys:user:save')"-->
            <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="state.dataListLoading" :data="state.dataList" border
              @selection-change="state.dataListSelectionChangeHandle" style="width: 100%" row-key="id"  @row-drop="onRowDrop">
      <el-table-column prop="name" label="项目名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="" :border="false" label="所属区域" header-align="center" align="center" width="240">
          <el-table-column prop="provinceName" colspan header-align="center" align="center" width="80"></el-table-column>
          <el-table-column prop="cityName" colspan="" header-align="center" align="center" width="80"></el-table-column>
          <el-table-column prop="creaName" rowspan header-align="center" align="center" width="80"></el-table-column>
      </el-table-column>
      <el-table-column prop="projectHead" label="施工负责人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="projectMember"  label="项目成员" header-align="center" align="center" width="80"></el-table-column>
<!--      <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column>-->
      <el-table-column prop="state" label="项目状态" header-align="center" align="center" width="80">
        <template v-slot="scope">
          <el-tag v-if="scope.row.state === 0" size="small" >施工中</el-tag>
          <el-tag v-if="scope.row.state === 1" size="small" type="warning">待验收</el-tag>
          <el-tag v-if="scope.row.state === 2" size="small" type="success">已验收</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="notes" label="项目描述" header-align="center" align="center" width="300"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="300">
        <template v-slot="scope">
          <el-button  type="primary" link
                     @click="addOrUpdateHandle(scope.row.id)">修改
          </el-button>
          <el-button  type="primary" link
                     @click="goToUser(scope.row)">成员管理
          </el-button>
          <el-button  type="primary" link
                     @click="goToLine(scope.row)">线路管理
          </el-button>
          <el-button  type="primary" link
                     @click="goToEquipment(scope.row)">定位点管理
          </el-button>
          <el-button  type="primary" link
                     @click="goToBaiduMap(scope.row)">项目监控
          </el-button>
          <el-button  type="primary" link
                     @click="migrateHandle(scope.row)">迁改
          </el-button>
          <el-button  type="primary" link
                     @click="state.deleteHandle(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                   :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="state.pageSizeChangeHandle"
                   @current-change="state.pageCurrentChangeHandle"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList">确定</add-or-update>
    <migrate ref="migrateRef" @refreshDataList="state.getDataList">确定</migrate>

  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import {onMounted, reactive, ref, toRefs} from "vue";
import migrate from "./project-migrate.vue";
import baseService from "@/service/baseService";
import {IObject} from "@/types/interface";
import { useRouter } from "vue-router";
import AddOrUpdate from "./project-add-or-update.vue";

const view = reactive({
  deleteIsBatch: true,
  getDataListURL: "/sysproject/project/page",
  getDataListIsPage: true,
  exportURL: "/sysproject/project/export",
  deleteURL: "/sysproject/project",
  provinceURL: "/sysproject/address/getLevel",
  cityURL: "/sysproject/address/getParentId",
  provinceOP: [],
  provOp: [],
  creaOp: [],
  pt: 0,
  pts: "",
  showTable:true
});
const state = reactive({...useView(view), ...toRefs(view)});

baseService.post(view.provinceURL, {"level": "1"}).then((res) => {
  view.provinceOP = res.data
});
const provinceChange = (item?: string) => {
  baseService.post(view.cityURL, {"id": item ? item : ""}).then((res) => {
    console.log(res.data)
    view.provOp = res.data;
    view.creaOp = [];
    state.dataForm.cityId = "";
    state.dataForm.creaId = "";
  });
};
const onRowDrop = (dropData: any, row: any) => {
  debugger
  const draggedItem = state.dataList.splice(dropData.draggedIndex, 1)[0];
  state.dataList.splice(dropData.droppedIndex, 0, draggedItem);
};
const cityChange = (item?: { id: "" }) => {
  baseService.post(view.cityURL, {"id": item ? item : ""}).then((res) => {
    Object.assign(view.creaOp, res.data);
    state.dataForm.creaId = "";
  });
};
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id);
};

const migrateRef = ref();
const migrateHandle = (row?: {}) => {
  migrateRef.value.init(row);
};

const router = useRouter();
//user equipment line
const goToUser = (tiem) => {
  router.replace({name:"user",params:{"projectId":tiem?tiem.id:"","projectName":tiem?tiem.name:""}});
};
const goToEquipment = (tiem) => {
  router.replace({name:"equipment",params:{"projectId":tiem?tiem.id:"","projectName":tiem?tiem.name:""}});
};
const goToLine= (tiem) => {
  router.replace({name:"line",params:{"projectId":tiem?tiem.id:"","projectName":tiem?tiem.name:""}});
};
const goToBaiduMap= (tiem) => {
  router.replace({name:"baiduMap",params:tiem});
};

</script>
